package android_testsuite.mytest.network_test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import android_testsuite.R;
import android_testsuite.mytest.util.Const;
import android_testsuite.mytest.util.Executor;

/**
 * @author Ren Hui
 * @since 1.0.1.058
 */
public class HttpActivity extends Activity implements View.OnClickListener {
    private EditText mEtThreadNum;
    private EditText mEtUrl;
    private Button mBtStart;
    private TextView mTxLoggingMsg;
    private Executor mExecutor;
    private String mErr;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Const.ON_LOG_MSG:
                    mTxLoggingMsg.setText(mTxLoggingMsg.getText() + (String) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 回调函数，先super。 父类走一边
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);
        this.mEtThreadNum = (EditText) findViewById(R.id.et_thread_number);
        this.mEtUrl = (EditText) findViewById(R.id.et_url);
        this.mBtStart = (Button) findViewById(R.id.bt_start);
        this.mTxLoggingMsg = (TextView) findViewById(R.id.tx_result);
        this.mBtStart.setOnClickListener(this);
        this.mEtUrl.setText("http://www.baidu.com");

        // 创建进程池对象
        mExecutor = new Executor(Const.MAX_POOL_SIZE);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.bt_start) {
            int threadNum;
            String number = this.mEtThreadNum.getText().toString();
            String url = this.mEtUrl.getText().toString();

            // 判断输入的数字是否正确
            boolean flag_ThreadNum = analyseNumberInput(number);
            boolean flag_Address = analyseAddressInput(url);
            if (flag_ThreadNum == true && flag_Address == true) {
                threadNum = Integer.valueOf(number);
                for (int i = 0; i < threadNum; i++) {
                    Runnable runnable = new HttpRunnable(this, url);
                    this.mExecutor.start(runnable);
                }
            } else {
                Toast.makeText(this, mErr, Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mExecutor.close();
    }

    public void onLogging(String msg) {
        this.mHandler.obtainMessage(Const.ON_LOG_MSG, msg).sendToTarget();
    }

    //地址输入检查
    private boolean analyseAddressInput(String mUrl) {
        // 不区分大小写
        String url = mUrl.toLowerCase();
        if (TextUtils.isEmpty(url)) {
            mErr = getResources().getString(R.string.empty);
            return false;
        } else if (((url.startsWith("http://") == true) && (url.length() > 7))
                || ((url.startsWith("https://") == true) && (url.length() > 8))) {
            return true;
        } else {
            mErr = getResources().getString(R.string.url_error);
            return false;
        }
    }

    //线程数输入检查
    private boolean analyseNumberInput(String number) {
        if (TextUtils.isEmpty(number)) {
            mErr = getResources().getString(R.string.empty);
            return false;
        } else {
            try {
                int res = Integer.valueOf(number);
                if (res < Const.MAX_THREAD) {
                    return true;
                } else {
                    mErr = getResources().getString(R.string.bigNumber) + Integer.toString(Const.MAX_THREAD);
                    return false;
                }
            } catch (NumberFormatException e) {
                Log.e("HttpActivity", e.toString());
                mErr = getResources().getString(R.string.notNumber);
                return false;
            }
        }
    }
}